const fs = require("fs");
const path = require("path");
const zlib = require("zlib");
const http = require("http");

const rsPath = path.resolve(__dirname, "../dist/1.txt");
const wsPath = path.resolve(__dirname, "../dist/4.gzip");
const htmlPath = path.resolve(__dirname, "../dist/index.html");

const gzip = zlib.createGzip();
const rs = fs.createReadStream(rsPath);
const ws = fs.createWriteStream(wsPath);
rs.pipe(gzip).pipe(ws);
ws.on("close", function () {
  console.log("end");
});

// http
//   .createServer((req, res) => {
//     console.log(req.url);
//     const rs = fs.createReadStream(htmlPath);

//     if (req.headers["accept-encoding"].indexOf("gzip") != -1) {
//       console.log(1);
//       res.writeHead(200, {
//         "content-encoding": "gzip",
//       });
//       rs.pipe(zlib.createGzip()).pipe(res);
//     } else {
//       console.log(2);
//       rs.pipe(res);
//     }
//   })
//   .listen(8080, () => {
//     console.log("http server is running on port http://localhost:8080");
//   });
